import 'package:shop/tools/storage.dart';

class SearchServices {
  //   保存历史记录
  static setHistoryData(keyWords) async {
    /*
   * 1.获取本地存储里面的数据（searchList）
   * */

    List? searchListData = await Storage.getData('searchList');

    if (searchListData != null) {
      // any 如果有一个就返回true
      var hasData = searchListData.any((v) {
        return v == keyWords;
      });
      if (!hasData) {
        searchListData.add(keyWords);
        await Storage.setData("searchList", searchListData);
      }
    } else {
      List tempList = [];
      tempList.add(keyWords);
      await Storage.setData("searchList", tempList);
    }
  }

  //   获取历史记录
  static Future<List> getHistoryData() async {
    List? searchListData = await Storage.getData("searchList");
    if (searchListData != null) {
      return searchListData;
    } else {
      return [];
    }
  }

  //     删除搜索记录
  static deleteHistoryData(keyWords) async {
    List? searchListData = await Storage.getData("searchList");
    if (searchListData != null) {
      Storage.removeData(keyWords);
      await Storage.setData("searchList", searchListData);
    }
  }

  //   清空搜索记录
  static clearHistoryData() async {
    await Storage.clear("searchList");
  }
}
